package com.group.activity.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group.common.utils.PageUtils;
import com.group.common.utils.Query;

import com.group.activity.dao.ShopTypeDao;
import com.group.activity.entity.ShopTypeEntity;
import com.group.activity.service.ShopTypeService;


@Service("shopTypeService")
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeDao, ShopTypeEntity> implements ShopTypeService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ShopTypeEntity> page = this.page(
                new Query<ShopTypeEntity>().getPage(params),
                new QueryWrapper<ShopTypeEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<ShopTypeEntity> selchild() {
        if (redisTemplate.opsForValue().get("shoplist")!=null){
            Object shoplist = redisTemplate.opsForValue().get("shoplist");
            String s = JSONObject.toJSONString(shoplist);
            List<ShopTypeEntity> shopTypeEntities=JSONObject.parseObject(s, new TypeReference<List<ShopTypeEntity>>(){

            });
            return shopTypeEntities;
        }
        List<ShopTypeEntity> shopTypeEntities = this.baseMapper.selectList(new QueryWrapper<ShopTypeEntity>().eq("shop_type_cid", 0));
        dg(shopTypeEntities);
        redisTemplate.opsForValue().set("shoplist",shopTypeEntities);
        redisTemplate.boundHashOps("ee");
        return shopTypeEntities;
    }

    private void dg(List<ShopTypeEntity> entityList){
        for (ShopTypeEntity shopTypeEntity : entityList) {

            List<ShopTypeEntity> cid = this.baseMapper.selectList(new QueryWrapper<ShopTypeEntity>().eq("shop_type_cid", shopTypeEntity.getShopTypeId()));
            if (cid!=null){
                shopTypeEntity.setList(cid);
                dg(cid);
            }
        }
    }
}